//
//  DHMembCheckController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/18.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHMembCheckController.h"
#import "DHMemberCardView.h"

@interface DHMembCheckController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scorllView;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
@property (weak, nonatomic) IBOutlet UILabel *yajinLabel;
@property (weak, nonatomic) IBOutlet UILabel *dingyueLabel;
@property (weak, nonatomic) IBOutlet UILabel *zhekouLabel;
@property (weak, nonatomic) IBOutlet UILabel *guwenLabel;
@property (weak, nonatomic) IBOutlet UILabel *liangtiLabel;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

@end

@implementation DHMembCheckController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"会员";
    
    self.cancelButton.layer.borderWidth = 1;
    self.cancelButton.layer.borderColor = [[UIColor colorWithHexString:@"#7b7b7b"] CGColor];
    self.cancelButton.layer.cornerRadius = self.cancelButton.height / 2;
    
    CGFloat discount = [[[GlobalManager shareGlobalManager].vip.info objectForKey:@"discount"] floatValue];
    if (discount >= 1) {
        self.zhekouLabel.text = @"无折扣";
    }else{
        self.zhekouLabel.text = [NSString stringWithFormat:@"%.0f折",discount * 100];
    }
    
    NSString *time = [[GlobalManager shareGlobalManager].vip.info objectForKey:@"expire"];
    NSDate *date = [NSDate dateWithServerISOString:time format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    self.timeLabel.text = [NSString stringWithFormat:@"%@",[NSDate stringFromDate:date withFormat:@"yyyy.MM.dd"]];
    
    int negativeDays = (int)[date daysAgo];
    int days = abs(negativeDays);
    self.dayLabel.text = [NSString stringWithFormat:@"剩余%i天",days];
    
    NSDictionary *obj = [GlobalManager shareGlobalManager].vip.info;
    CGFloat spaceX = (self.scorllView.width - 260) / 2;
    CGFloat spaceY = (self.scorllView.height - 143) / 2;
    DHMemberCardView *card = [DHMemberCardView shareDHMemberCardView];
    card.frame = CGRectMake(spaceX , spaceY, 260, 143);
    if ([[obj objectForKey:@"name"] isEqualToString:@"租衣包月"]) {
        card.imgView.image = [UIImage imageNamed:@"month"];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣季卡"]){
        card.imgView.image = [UIImage imageNamed:@"season"];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣半年卡"]){
        card.imgView.image = [UIImage imageNamed:@"half_year"];
    }else if ([[obj objectForKey:@"name"] isEqualToString:@"租衣年卡"]){
        card.imgView.image = [UIImage imageNamed:@"year"];
    }
    card.nameLabel.text = [obj objectForKey:@"name"];
    card.usedLabel.hidden = NO;
    
    NSString *startTime = [[GlobalManager shareGlobalManager].vip.info objectForKey:@"begin"];
    NSDate *startDate = [NSDate dateWithServerISOString:startTime format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    NSString *startTimeFormat = [NSDate stringFromDate:startDate withFormat:@"yyyy.MM.dd"];
    card.timeLabel.text = [NSString stringWithFormat:@"%@ - %@",startTimeFormat, [NSDate stringFromDate:date withFormat:@"yyyy.MM.dd"]];
    
    [self.scorllView addSubview:card];
    
    [self loadData];
}

- (void)loadData{
    [[DHNetAPIClient shareDHNetAPIClient] request_user_measure_lastAndBlock:^(id data, NSError *error) {
        if (data) {
            NSInteger count = [[data objectForKey:@"count"] integerValue];
            self.liangtiLabel.text = [NSString stringWithFormat:@"本月剩余%ld次",(long)count];
        }
    }];
    
    [[DHNetAPIClient shareDHNetAPIClient] request_user_consult_lastAndBlock:^(id data, NSError *error) {
        if (data) {
            NSInteger count = [[data objectForKey:@"count"] integerValue];
            self.guwenLabel.text = [NSString stringWithFormat:@"本月剩余%ld次",(long)count];
        }
    }];
    
    [[DHNetAPIClient shareDHNetAPIClient] request_user_lease_lastAndBlock:^(id data, NSError *error) {
        if (data) {
            NSInteger count = [[data objectForKey:@"last"] integerValue];
            self.dingyueLabel.text = [NSString stringWithFormat:@"本月剩余%ld件服饰订阅数量",(long)count];
        }
    }];
    
    [[DHNetAPIClient shareDHNetAPIClient] request_user_depositAndBlock:^(id data, NSError *error) {
        CGFloat deposit = [[data objectForKey:@"deposit"] floatValue];
        self.yajinLabel.text = [NSString stringWithFormat:@"%.1f元",deposit];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
